package com.itheima.udpguang;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/*
udp 的接收端
 */
public class ReceiveClient {

    public static void main(String[] args) throws IOException {
        //创建对象
        DatagramSocket ds = new DatagramSocket(8888);
        while (true) {
            //容器
            byte[] bytes = new byte[1024 * 64];
            DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
            //接收数据
            ds.receive(dp);
            //接收到的数据的长度
            int length = dp.getLength();
            InetAddress address = dp.getAddress();
            //查看数据
            System.out.println("来自"+address.getHostName()+"ip为"+address.getHostAddress()+"说了:"+new String(bytes,0,length));
        }

        //释放资源
       // ds.close();

    }
}
